package com.sgq.vue.service.system.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sgq.vue.entity.system.Department;
import com.sgq.vue.dao.system.DepartmentMapper;
import com.sgq.vue.service.system.DepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sgq.vue.utils.system.DepartmentTree;
import com.sgq.vue.vo.query.DepartmentQueryVo;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author SGQ
 * @since 2023-10-30
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {

    @Override
    public List<Department> findDepartmentList(DepartmentQueryVo departmentQueryVo) {

        LambdaQueryWrapper<Department> wrapper = new LambdaQueryWrapper<>();

        wrapper.like(!ObjectUtils.isEmpty(departmentQueryVo),Department::getDepartmentName,departmentQueryVo.getDepartmentName());

        wrapper.orderByAsc(Department::getOrderNum);

        List<Department> departments = this.baseMapper.selectList(wrapper);

        return departments;
    }

    @Override
    public List<Department> findParentDepartment() {
        //创建条件构造器对象
        QueryWrapper<Department> queryWrapper = new QueryWrapper<Department>();
        //排序
        queryWrapper.orderByAsc("order_num" );
        //查询部门列表
        List<Department> departmentList = baseMapper.selectList(queryWrapper);
        //创建部门对象
        Department department = new Department();
        department.setId(0L);
        department.setDepartmentName("顶级部门");
        department.setPid(-1L);
        departmentList.add(department);
        //生成部门菜单树
        List<Department> departmentTree = DepartmentTree.makeDepartmentTree(departmentList, -1L);
        return departmentTree;

    }

    @Override
    public Boolean check(Long id) {
        LambdaQueryWrapper<Department> queryWrapper = new LambdaQueryWrapper<>();
        //是否存在把我的id作为父id的对象
        queryWrapper.eq(Department::getPid,id);
        Long count = this.baseMapper.selectCount(queryWrapper);
        return count > 0 ?  true : false;
    }
}
